//
//  NewViewController.swift
//  我的微博
//
//  Created by 吴加将 on 15/9/9.
//  Copyright © 2015年 吴加将. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class NewViewController: UICollectionViewController {
   private var layout = NewLayout()
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
    init(){
        super.init(collectionViewLayout:layout)
        
    }

    required init?(coder aDecoder: NSCoder) {
        //fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
self.collectionView!.registerClass(NewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        

        // Do any additional setup after loading the view.
    }



    // MARK: UICollectionViewDataSource

    let imageNumber = 4
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return imageNumber
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewCell
        cell.imageIndex=indexPath.item
        
        return cell
    }
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        let index = collectionView.indexPathsForVisibleItems().last!
        if index.item==imageNumber-1{
            (collectionView.cellForItemAtIndexPath(index) as! NewCell).showBtn()
            
        }
    }
//    func clickBtn(){
//        print(__FUNCTION__)
//       // presentViewController(HomeViewController, animated:true, completion: nil)
//        let nav = UINavigationController(rootViewController: HomeViewController())
//        presentViewController(nav, animated: true, completion: nil)
//
//    }
}
    // MARK: - 自定义cell
class NewCell : UICollectionViewCell {
    var imageIndex : Int = 0 {
        didSet{
          iconview.image=UIImage(named: "new_feature_\(imageIndex + 1)")
            btn.hidden=true
        }
    }
    //
        override init(frame: CGRect) {
            super.init(frame: frame)
            addSubview(iconview)
            addSubview(btn)
            //自动布局
            iconview.translatesAutoresizingMaskIntoConstraints=false
            addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[iconview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["iconview":iconview]))
            addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[iconview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["iconview":iconview]))
            btn.translatesAutoresizingMaskIntoConstraints=false
            addConstraint(NSLayoutConstraint(item: btn, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -160))
            addConstraint(NSLayoutConstraint(item: btn, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
            }

        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    //按钮动画
    func showBtn(){
        btn.hidden=false
        btn.transform=CGAffineTransformMakeScale(0,0)
        btn.userInteractionEnabled=false

        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10.0, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.btn.transform=CGAffineTransformIdentity
            }) { _ in
                self.btn.userInteractionEnabled=true
        }
    }
    //懒加载iconview
    lazy var iconview = UIImageView()
    lazy var btn : UIButton = {
        let btn = UIButton()
    btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
    btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
    btn.addTarget(self, action: "clickBtn", forControlEvents: UIControlEvents.TouchUpInside)
    btn.setTitle("开始体验", forState: UIControlState.Normal)
     // btn.hidden=true
        return btn
    }()
//    func clickBtn(){
//        print(__FUNCTION__)
//        
//    }
    func clickBtn(){
        print(__FUNCTION__)
 
    }}
  // MARK: -自定义布局
 private class NewLayout : UICollectionViewFlowLayout{
    private override func prepareLayout() {
        itemSize=collectionView!.bounds.size
        minimumInteritemSpacing=0
        minimumLineSpacing=0
        scrollDirection=UICollectionViewScrollDirection.Horizontal
        collectionView?.pagingEnabled=true
        collectionView?.showsHorizontalScrollIndicator=false
        collectionView?.bounces=false
    }
    
}



